'''
Created on 2010/09/08

@author: yukiyonezawa
'''
from pyrfid.DataProtocol.Driver.VirtualTag.VirtualTag import VirtualTag

class VirtualUHFTag(VirtualTag):
    """
    The start_address is world.
    """    
    __UII = ["3223", "", "", "", "", "", "", "", "", "", "", "", "" ]
    __UHF_ACCESS_WIDTH = 4
        
    def __init__(self, tag_id):
        VirtualTag.__init__(self, tag_id, self.__UHF_ACCESS_WIDTH)
    
    def updateUII(self, start_address, write_data):        
        #check User_Memory
        remain_address = (len(self.__UII) - (start_address + (len(write_data) / self.__UHF_ACCESS_WIDTH) )) * -1
        for index in range(remain_address):
            self.__UII.append("")

        #insert data to USER_MEMORY
        for index in range(len(write_data) / self.__UHF_ACCESS_WIDTH):
            self.__UII[start_address + index] = write_data[(index * self.__UHF_ACCESS_WIDTH):(index * self.__UHF_ACCESS_WIDTH) + self.__UHF_ACCESS_WIDTH]        
        print "UII Memory===================================="
        print self.__UII
        print "==============================================="
        
    def getUII(self, start_address, width):        
        if len(self.__UII) <= start_address + width:
            raise "undefined on UII"
        
        return_value = ""
        for index in range(width):
            return_value += self.__UII[start_address + index]        
        return return_value
            
    def clearUII(self):
        self.__UII = ""

if __name__ == '__main__':
    tag = VirtualUHFTag("HELLO WORLD")
    print tag
    tag.updateUII(0, "108020803080408050806080")
    print tag.getUII(2, 40)
    
